﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Drawing;

namespace MipsPipelineSimulator
{
    class IDStage
    {
        //能否运行
        static public int enableRun = 0;

        static private ManualResetEvent manuaEevent = null;

        static public ManualResetEvent bzManualEvent = null;

        //IFStage必须先读
        static public ManualResetEvent nopNumManualEvent = null;

        static public ManualResetEvent nopDrawManualEvent = null;

        static public ManualResetEvent registerReadManualEvent = null;

        static public ManualResetEvent idDrawManualEvent = null;

        static public Int64 bzAddress = 0;

        static public bool bzSuccess = false;

        static public int nopNum = 0;

        static private int runTimes = 0;

        static private Thread idThread = null;

        static public Point[] position = new Point[44];

        static private MainForm mainForm = null;

        static public Code code;

        static public object[] args = null;

        static private Dictionary<string, int> dicTmp = null;

        static private void ThreadFun()
        {
            runTimes++;
            //判断是否在暂停
            if (nopNum > 0)
            {
                enableRun = 0;
                IDStage.nopDrawManualEvent.Set();
                manuaEevent.Set();
                IDStage.nopNumManualEvent.WaitOne();
                nopNum--;
                return;
            }

            //判断可不可以运行
            if (enableRun < 0)
            {
                IDStage.nopDrawManualEvent.Set();
                manuaEevent.Set();
                return;
            }

            

            nopNum = IDStage.VlidateDataConflict(IDStage.code);
            IDStage.nopDrawManualEvent.Set();

            if(nopNum > 0)
            {
                IDStage.nopNumManualEvent.WaitOne();
                nopNum--;
                 manuaEevent.Set();
                return;
            }

            IDStage.enableRun--;

            registerReadManualEvent.WaitOne();

            object obj1 = null;
            object obj2 = null;
            object obj3 = null;
            string str1 = null;
            string str2 = null;
            string str3 = null;
            string str4 = null;
            string str5 = null;
            string str6 = null;

            switch (code.codeType)
            {
                case CodeType.ADD:
                case CodeType.SUB:
                case CodeType.MUL:
                case CodeType.SLT:
                case CodeType.AND:
                case CodeType.NOR:
                case CodeType.OR:
                case CodeType.XOR:
                case CodeType.SLLV:
                case CodeType.SRAV:
                case CodeType.SRLV:
                    {
                        string strArg1 = mainForm.commandForm.registerControl.GetRegisterValue((string)code.args[0]);
                        string strArg2 = mainForm.commandForm.registerControl.GetRegisterValue((string)code.args[1]);
                        obj1 = (Int32)CommonTool.StrToNum(TypeCode.Int32, strArg1.Substring(8), 16);
                        obj2 = (Int32)CommonTool.StrToNum(TypeCode.Int32, strArg2.Substring(8), 16);
                        str1 = CommonTool.NumToStr(TypeCode.Int32, obj1, "D", false);
                        str2 = CommonTool.NumToStr(TypeCode.Int32, obj2, "D", false);
                        break;
                    }
                case CodeType.DADD:
                case CodeType.DSUB:
                case CodeType.DSLLV:
                case CodeType.DSRLV:
                case CodeType.DSRAV:
                    {
                        string strArg1 = mainForm.commandForm.registerControl.GetRegisterValue((string)code.args[0]);
                        string strArg2 = mainForm.commandForm.registerControl.GetRegisterValue((string)code.args[1]);
                        obj1 = (Int32)CommonTool.StrToNum(TypeCode.Int64, strArg1, 16);
                        obj2 = (Int32)CommonTool.StrToNum(TypeCode.Int64, strArg2, 16);
                        str1 = CommonTool.NumToStr(TypeCode.Int64, obj1, "D", false);
                        str2 = CommonTool.NumToStr(TypeCode.Int64, obj2, "D", false);
                        break;
                    }
                case CodeType.ADD_S:
                case CodeType.SUB_S:
                case CodeType.MUL_S:
                case CodeType.DIV_S:
                    {
                        string strArg1 = mainForm.commandForm.registerControl.GetRegisterValue((string)code.args[0]);
                        string strArg2 = mainForm.commandForm.registerControl.GetRegisterValue((string)code.args[1]);
                        obj1 = (float)CommonTool.StrToNum(TypeCode.Single, strArg1.Substring(8), 16);
                        obj2 = (float)CommonTool.StrToNum(TypeCode.Single, strArg2.Substring(8), 16);
                        str1 = CommonTool.NumToStr(TypeCode.Single, obj1, "D", false);
                        str2 = CommonTool.NumToStr(TypeCode.Single, obj2, "D", false);
                        break;
                    }
                case CodeType.ADD_D:
                case CodeType.SUB_D:
                case CodeType.MUL_D:
                case CodeType.DIV_D:
                    {
                        string strArg1 = mainForm.commandForm.registerControl.GetRegisterValue((string)code.args[0]);
                        string strArg2 = mainForm.commandForm.registerControl.GetRegisterValue((string)code.args[1]);
                        obj1 = (double)CommonTool.StrToNum(TypeCode.Double, strArg1, 16);
                        obj2 = (double)CommonTool.StrToNum(TypeCode.Double, strArg2, 16);
                        str1 = CommonTool.NumToStr(TypeCode.Double, obj1, "D", false);
                        str2 = CommonTool.NumToStr(TypeCode.Double, obj2, "D", false);
                        break;
                    }
                case CodeType.ADDU:
                case CodeType.SUBU:
                case CodeType.SLTU:
                    {
                        string strArg1 = mainForm.commandForm.registerControl.GetRegisterValue((string)code.args[0]);
                        string strArg2 = mainForm.commandForm.registerControl.GetRegisterValue((string)code.args[1]);
                        obj1 = (Int32)CommonTool.StrToNum(TypeCode.UInt32, strArg1.Substring(8), 16);
                        obj2 = (Int32)CommonTool.StrToNum(TypeCode.UInt32, strArg2.Substring(8), 16);
                        str1 = CommonTool.NumToStr(TypeCode.UInt32, obj1, "D", false);
                        str2 = CommonTool.NumToStr(TypeCode.UInt32, obj2, "D", false);
                        break;
                    }

                case CodeType.DADDU:
                case CodeType.DSUBU:
                    {
                        string strArg1 = mainForm.commandForm.registerControl.GetRegisterValue((string)code.args[0]);
                        string strArg2 = mainForm.commandForm.registerControl.GetRegisterValue((string)code.args[1]);
                        obj1 = (UInt64)CommonTool.StrToNum(TypeCode.UInt64, strArg1, 16);
                        obj2 = (UInt64)CommonTool.StrToNum(TypeCode.UInt64, strArg2, 16);
                        str1 = CommonTool.NumToStr(TypeCode.UInt64, obj1, "D", false);
                        str2 = CommonTool.NumToStr(TypeCode.UInt64, obj2, "D", false);
                        break;
                    }
                case CodeType.ADDI:
                case CodeType.SLTI:
                case CodeType.ANDI:
                case CodeType.ORI:
                case CodeType.XORI:
                case CodeType.SLL:
                case CodeType.SRA:
                case CodeType.SRL:
                    {
                        string strArg1 = mainForm.commandForm.registerControl.GetRegisterValue((string)code.args[0]);
                        obj1 = (Int32)CommonTool.StrToNum(TypeCode.Int32, strArg1.Substring(8), 16);
                        obj2 = Convert.ToInt32(code.args[1]);
                        str1 = CommonTool.NumToStr(TypeCode.Int32, obj1, "D", false);
                        str2 = CommonTool.NumToStr(TypeCode.Int32, obj2, "D", false);
                        str3 = CommonTool.NumToStr(TypeCode.Int16, code.args[1], "D", false);
                        break;
                    }
                case CodeType.DADDI:
                case CodeType.DSLL:
                case CodeType.DSRL:
                case CodeType.DSRA:
                    {
                        string strArg1 = mainForm.commandForm.registerControl.GetRegisterValue((string)code.args[0]);
                        obj1 = (Int64)CommonTool.StrToNum(TypeCode.Int64, strArg1.Substring(8), 16);
                        obj2 = Convert.ToInt64(code.args[1]);
                        str1 = CommonTool.NumToStr(TypeCode.Int64, obj1, "D", false);
                        str2 = CommonTool.NumToStr(TypeCode.Int64, obj2, "D", false);
                        str3 = CommonTool.NumToStr(TypeCode.Int16, code.args[1], "D", false);
                        break;
                    }
                case CodeType.ADDIU:
                case CodeType.SLTIU:
                    {
                        string strArg1 = mainForm.commandForm.registerControl.GetRegisterValue((string)code.args[0]);
                        obj1 = (UInt32)CommonTool.StrToNum(TypeCode.UInt32, strArg1.Substring(8), 16);
                        obj2 = Convert.ToUInt32(code.args[1]);
                        str1 = CommonTool.NumToStr(TypeCode.UInt32, obj1, "D", false);
                        str2 = CommonTool.NumToStr(TypeCode.UInt32, obj2, "D", false);
                        str3 = CommonTool.NumToStr(TypeCode.UInt16, code.args[1], "D", false);
                        break;
                    }
                case CodeType.DADDIU:
                    {
                        string strArg1 = mainForm.commandForm.registerControl.GetRegisterValue((string)code.args[0]);
                        obj1 = (UInt64)CommonTool.StrToNum(TypeCode.UInt64, strArg1, 16);
                        obj2 = Convert.ToUInt64(code.args[1]);
                        str1 = CommonTool.NumToStr(TypeCode.UInt64, obj1, "D", false);
                        str2 = CommonTool.NumToStr(TypeCode.UInt64, obj2, "D", false);
                        str3 = CommonTool.NumToStr(TypeCode.UInt16, code.args[1], "D", false);
                        break;
                    }
                case CodeType.BEQZ:
                case CodeType.BNEZ:
                    {
                        string strArg1 = mainForm.commandForm.registerControl.GetRegisterValue((string)code.args[0]);
                        obj1 = (Int32)CommonTool.StrToNum(TypeCode.Int32, strArg1.Substring(8), 16);
                        str1 = CommonTool.NumToStr(TypeCode.Int32, obj1, "D", false);
                        obj2 = Convert.ToInt64(code.args[1]);
                        str2 = CommonTool.NumToStr(TypeCode.Int64, obj2, "D", false);
                        str3 = CommonTool.NumToStr(TypeCode.Int16, code.args[1], "D", false);
                        if (code.codeType == CodeType.BEQZ)
                        {
                            if ((Int32)obj1 == 0) bzSuccess = true;
                            else bzSuccess = false;
                        }
                        else
                        {
                            if ((Int32)obj1 == 0) bzSuccess = false;
                            else bzSuccess = true;
                        }
                        bzAddress = (Int64)IDStage.args[0] + (Int64)obj2;
                        str4 = CommonTool.NumToStr(TypeCode.Int64, IDStage.args[0], "D", false);
                        str5 = CommonTool.NumToStr(TypeCode.Int64, bzAddress, "D", false);
                        str6 = bzSuccess.ToString();
                        break;
                    }
                case CodeType.LB:
                case CodeType.LH:
                case CodeType.LW:
                case CodeType.LBU:
                case CodeType.LHU:
                case CodeType.LWU:
                case CodeType.LD:
                case CodeType.LDC1:
                case CodeType.LWC1:
                    {
                        string strArg1 = mainForm.commandForm.registerControl.GetRegisterValue((string)code.args[0]);
                        obj1 = (Int64)CommonTool.StrToNum(TypeCode.Int64, strArg1, 16);
                        str1 = CommonTool.NumToStr(TypeCode.Int64, obj1, "D", false);
                        obj2 = Convert.ToInt64(code.args[1]);
                        str2 = CommonTool.NumToStr(TypeCode.Int64, obj2, "D", false);
                        str3 = CommonTool.NumToStr(TypeCode.Int16, code.args[1], "D", false);
                        break;
                    }
                
                case CodeType.SD:
                    {
                        string strArg1 = mainForm.commandForm.registerControl.GetRegisterValue((string)code.args[0]);
                        obj1 = (Int64)CommonTool.StrToNum(TypeCode.Int64, strArg1, 16);
                        str1 = CommonTool.NumToStr(TypeCode.Int64, obj1, "D", false);
                        obj2 = Convert.ToInt64(code.args[1]);
                        str2 = CommonTool.NumToStr(TypeCode.Int64, obj2, "D", false);
                        str3 = CommonTool.NumToStr(TypeCode.Int16, code.args[1], "D", false);
                        string strArg2 = mainForm.commandForm.registerControl.GetRegisterValue((string)code.args[2]);
                        obj3 = (Int32)CommonTool.StrToNum(TypeCode.Int64, strArg2, 16);
                        str4 = CommonTool.NumToStr(TypeCode.Int64, obj3, "D", false);
                        break;
                    }
                case CodeType.SB:
                    {
                        string strArg1 = mainForm.commandForm.registerControl.GetRegisterValue((string)code.args[0]);
                        obj1 = (Int64)CommonTool.StrToNum(TypeCode.Int64, strArg1, 16);
                        str1 = CommonTool.NumToStr(TypeCode.Int64, obj1, "D", false);
                        obj2 = Convert.ToInt64(code.args[1]);
                        str2 = CommonTool.NumToStr(TypeCode.Int64, obj2, "D", false);
                        str3 = CommonTool.NumToStr(TypeCode.Int16, code.args[1], "D", false);
                        string strArg2 = mainForm.commandForm.registerControl.GetRegisterValue((string)code.args[2]);
                        obj3 = (byte)CommonTool.StrToNum(TypeCode.SByte, strArg2.Substring(14), 16);
                        str4 = CommonTool.NumToStr(TypeCode.SByte, obj3, "D", false);
                        break;
                    }
                case CodeType.SH:
                    {
                        string strArg1 = mainForm.commandForm.registerControl.GetRegisterValue((string)code.args[0]);
                        obj1 = (Int64)CommonTool.StrToNum(TypeCode.Int64, strArg1, 16);
                        str1 = CommonTool.NumToStr(TypeCode.Int64, obj1, "D", false);
                        obj2 = Convert.ToInt64(code.args[1]);
                        str2 = CommonTool.NumToStr(TypeCode.Int64, obj2, "D", false);
                        str3 = CommonTool.NumToStr(TypeCode.Int16, code.args[1], "D", false);
                        string strArg2 = mainForm.commandForm.registerControl.GetRegisterValue((string)code.args[2]);
                        obj3 = (short)CommonTool.StrToNum(TypeCode.Int16, strArg2.Substring(12), 16);
                        str4 = CommonTool.NumToStr(TypeCode.Int16, obj3, "D", false);
                        break;
                    }
                case CodeType.SW:
                    {
                        string strArg1 = mainForm.commandForm.registerControl.GetRegisterValue((string)code.args[0]);
                        obj1 = (Int64)CommonTool.StrToNum(TypeCode.Int64, strArg1, 16);
                        str1 = CommonTool.NumToStr(TypeCode.Int64, obj1, "D", false);
                        obj2 = Convert.ToInt64(code.args[1]);
                        str2 = CommonTool.NumToStr(TypeCode.Int64, obj2, "D", false);
                        str3 = CommonTool.NumToStr(TypeCode.Int16, code.args[1], "D", false);
                        string strArg2 = mainForm.commandForm.registerControl.GetRegisterValue((string)code.args[2]);
                        obj3 = (Int32)CommonTool.StrToNum(TypeCode.Int32, strArg2.Substring(8), 16);
                        str4 = CommonTool.NumToStr(TypeCode.Int32, obj3, "D", false);
                        break;
                    }
                case CodeType.SDC1:
                    {
                        string strArg1 = mainForm.commandForm.registerControl.GetRegisterValue((string)code.args[0]);
                        obj1 = (Int64)CommonTool.StrToNum(TypeCode.Int64, strArg1, 16);
                        str1 = CommonTool.NumToStr(TypeCode.Int64, obj1, "D", false);
                        obj2 = Convert.ToInt64(code.args[1]);
                        str2 = CommonTool.NumToStr(TypeCode.Int64, obj2, "D", false);
                        str3 = CommonTool.NumToStr(TypeCode.Int16, code.args[1], "D", false);
                        string strArg2 = mainForm.commandForm.registerControl.GetRegisterValue((string)code.args[2]);
                        obj3 = (float)CommonTool.StrToNum(TypeCode.Single, strArg2.Substring(8), 16);
                        str4 = CommonTool.NumToStr(TypeCode.Single, obj3, "D", false);
                        break;
                    }
                case CodeType.SWC1:
                    {
                        string strArg1 = mainForm.commandForm.registerControl.GetRegisterValue((string)code.args[0]);
                        obj1 = (Int64)CommonTool.StrToNum(TypeCode.Int64, strArg1, 16);
                        str1 = CommonTool.NumToStr(TypeCode.Int64, obj1, "D", false);
                        obj2 = Convert.ToInt64(code.args[1]);
                        str2 = CommonTool.NumToStr(TypeCode.Int64, obj2, "D", false);
                        str3 = CommonTool.NumToStr(TypeCode.Int16, code.args[1], "D", false);
                        string strArg2 = mainForm.commandForm.registerControl.GetRegisterValue((string)code.args[2]);
                        obj3 = (double)CommonTool.StrToNum(TypeCode.Double, strArg2, 16);
                        str4 = CommonTool.NumToStr(TypeCode.Double, obj3, "D", false);
                        break;
                    }
            }

            idDrawManualEvent.WaitOne();

            switch (code.codeType)
            {
                case CodeType.ADD:
                case CodeType.SUB:
                case CodeType.MUL:
                case CodeType.SLT:
                case CodeType.DADD:
                case CodeType.DSUB:
                case CodeType.AND:
                case CodeType.NOR:
                case CodeType.OR:
                case CodeType.XOR:
                case CodeType.ADD_S:
                case CodeType.ADD_D:
                case CodeType.SUB_S:
                case CodeType.SUB_D:
                case CodeType.MUL_S:
                case CodeType.MUL_D:
                case CodeType.DIV_S:
                case CodeType.DIV_D:
                case CodeType.SLLV:
                case CodeType.SRAV:
                case CodeType.SRLV:
                case CodeType.DSLLV:
                case CodeType.DSRLV:
                case CodeType.DSRAV:
                case CodeType.ADDU:
                case CodeType.SUBU:
                case CodeType.SLTU:
                case CodeType.DADDU:
                case CodeType.DSUBU:
                    {
                        Thread thread1 = CommonTool.StartDrawPathsThread(new DrawPaths(
                           new Animate[2] { 
                                CommonTool.SetAnimate(IDStage.position[18],IDStage.position[19],Direction.LeftToRight,(string)code.args[0]+"/"+(string)code.args[1],true,mainForm.GetGraphics()),
                                CommonTool.SetAnimate(IDStage.position[19],IDStage.position[16],Direction.BottomToTop,(string)code.args[0]+"/"+(string)code.args[1],true,mainForm.GetGraphics())
                            }, IDStage.mainForm));
                        thread1.Join();
                        thread1 = null;

                        Thread thread2 = CommonTool.StartDrawPathsThread(new DrawPaths(
                           new Animate[2] { 
                                CommonTool.SetAnimate(IDStage.position[16],IDStage.position[14],Direction.BottomToTop,(string)code.args[0],true,mainForm.GetGraphics()),
                                CommonTool.SetAnimate(IDStage.position[14],IDStage.position[15],Direction.LeftToRight,(string)code.args[0],true,mainForm.GetGraphics())
                            }, IDStage.mainForm));

                        Thread thread3 = CommonTool.StartDrawPathsThread(new DrawPaths(
                           new Animate[1] { 
                                CommonTool.SetAnimate(IDStage.position[16],IDStage.position[17],Direction.LeftToRight,(string)code.args[1],true,mainForm.GetGraphics())
                            }, IDStage.mainForm));

                        Thread thread4 = CommonTool.StartDrawPathsThread(new DrawPaths(
                           new Animate[2] { 
                                CommonTool.SetAnimate(IDStage.position[19],IDStage.position[34],Direction.TopToBottom,code.codeType.ToString(),true,mainForm.GetGraphics()),
                                CommonTool.SetAnimate(IDStage.position[34],IDStage.position[37],Direction.LeftToRight,code.codeType.ToString(),true,mainForm.GetGraphics())
                            }, IDStage.mainForm));
                        thread3.Join();
                        thread3 = null;
                        thread2.Join();
                        thread2 = null;

                        Thread thread5 = CommonTool.StartDrawPathsThread(new DrawPaths(
                           new Animate[1] { 
                                CommonTool.SetAnimate(IDStage.position[20],IDStage.position[22],Direction.LeftToRight,str1,true,mainForm.GetGraphics())
                            }, IDStage.mainForm));
                        Thread thread6 = CommonTool.StartDrawPathsThread(new DrawPaths(
                           new Animate[1] { 
                                CommonTool.SetAnimate(IDStage.position[27],IDStage.position[28],Direction.LeftToRight,str2,true,mainForm.GetGraphics())
                            }, IDStage.mainForm));
                        thread5.Join();
                        thread5 = null;
                        thread6.Join();
                        thread6 = null;
                        thread4.Join();
                        thread4 = null;

                        Thread thread7 = CommonTool.StartBlinkThread(new Blink(
                            str1, IDStage.position[38], DrawTool.blinkTimes, DrawTool.blinkSpanTime, DrawTool.blinkHideTime, IDStage.mainForm));

                        Thread thread8 = CommonTool.StartBlinkThread(new Blink(
                            str2, IDStage.position[39], DrawTool.blinkTimes, DrawTool.blinkSpanTime, DrawTool.blinkHideTime, IDStage.mainForm));

                        Thread thread9 = CommonTool.StartBlinkThread(new Blink(
                           code.codeType.ToString(), IDStage.position[41], DrawTool.blinkTimes, DrawTool.blinkSpanTime, DrawTool.blinkHideTime, IDStage.mainForm));

                        thread7.Join();
                        thread7 = null;
                        thread8.Join();
                        thread8 = null;
                        thread9.Join();
                        thread9 = null;

                        CommonTool.DrawPath(IDStage.mainForm,
                            new DrawPath[5]{
                                 new DrawPath(mainForm.GetGraphics(),new Point[4]{IDStage.position[18],IDStage.position[19],IDStage.position[34],IDStage.position[37]},DrawTool.orignalPen),
                                 new DrawPath(mainForm.GetGraphics(),new Point[3]{IDStage.position[19],IDStage.position[16],IDStage.position[17]},DrawTool.orignalPen),
                                 new DrawPath(mainForm.GetGraphics(),new Point[3]{IDStage.position[16],IDStage.position[14],IDStage.position[15]},DrawTool.orignalPen),
                                 new DrawPath(mainForm.GetGraphics(),new Point[2]{IDStage.position[20],IDStage.position[22]},DrawTool.orignalPen),
                                 new DrawPath(mainForm.GetGraphics(),new Point[2]{IDStage.position[27],IDStage.position[28]},DrawTool.orignalPen)
                            });

                        EXStage.Wait();
                        EXStage.code = IDStage.code;
                        EXStage.args = new object[2] { obj1, obj2};
                        EXStage.enableRun++;
                        break;
                    }
                case CodeType.ADDI:
                case CodeType.SLTI:
                case CodeType.DADDI:
                case CodeType.DADDIU:
                case CodeType.ANDI:
                case CodeType.ORI:
                case CodeType.XORI:
                case CodeType.SLL:
                case CodeType.SRA:
                case CodeType.SRL:
                case CodeType.DSLL:
                case CodeType.DSRL:
                case CodeType.DSRA:
                case CodeType.LB:
                case CodeType.LD:
                case CodeType.LH:
                case CodeType.LW:
                case CodeType.LBU:
                case CodeType.LHU:
                case CodeType.LWU:
                case CodeType.LDC1:
                case CodeType.LWC1:
                    {
                        Thread thread1 = CommonTool.StartDrawPathsThread(new DrawPaths(
                                                   new Animate[1] { 
                                CommonTool.SetAnimate(IDStage.position[18],IDStage.position[19],Direction.LeftToRight,(string)code.args[0]+"/"+str3,true,mainForm.GetGraphics())
                            }, IDStage.mainForm));
                        thread1.Join();
                        thread1 = null;

                        Thread thread2 = CommonTool.StartDrawPathsThread(new DrawPaths(
                           new Animate[2] { 
                                CommonTool.SetAnimate(IDStage.position[19],IDStage.position[14],Direction.BottomToTop,(string)code.args[0],true,mainForm.GetGraphics()),
                                CommonTool.SetAnimate(IDStage.position[14],IDStage.position[15],Direction.LeftToRight,(string)code.args[0],true,mainForm.GetGraphics())
                            }, IDStage.mainForm));

                        Thread thread3 = CommonTool.StartDrawPathsThread(new DrawPaths(
                           new Animate[1] { 
                                CommonTool.SetAnimate(IDStage.position[19],IDStage.position[29],Direction.TopToBottom,code.codeType.ToString() + "/" +  str3,true,mainForm.GetGraphics())
                            }, IDStage.mainForm));
                        thread3.Join();
                        thread3 = null;

                        Thread thread4 = CommonTool.StartDrawPathsThread(new DrawPaths(
                           new Animate[2] { 
                                CommonTool.SetAnimate(IDStage.position[29],IDStage.position[34],Direction.TopToBottom,code.codeType.ToString(),true,mainForm.GetGraphics()),
                                CommonTool.SetAnimate(IDStage.position[34],IDStage.position[37],Direction.LeftToRight,code.codeType.ToString(),true,mainForm.GetGraphics())
                            }, IDStage.mainForm));

                        Thread thread5 = CommonTool.StartDrawPathsThread(new DrawPaths(
                                                   new Animate[1] { 
                                CommonTool.SetAnimate(IDStage.position[29],IDStage.position[30],Direction.LeftToRight,str3,true,mainForm.GetGraphics())
                            }, IDStage.mainForm));
                        thread5.Join();
                        thread5 = null;
                        thread2.Join();
                        thread2 = null;

                        Thread thread6 = CommonTool.StartDrawPathsThread(new DrawPaths(
                                                   new Animate[1] { 
                                CommonTool.SetAnimate(IDStage.position[20],IDStage.position[22],Direction.LeftToRight,str1,true,mainForm.GetGraphics())
                            }, IDStage.mainForm));
                        Thread thread7 = CommonTool.StartDrawPathsThread(new DrawPaths(
                                                   new Animate[1] { 
                                CommonTool.SetAnimate(IDStage.position[31],IDStage.position[33],Direction.LeftToRight,str2,true,mainForm.GetGraphics())
                            }, IDStage.mainForm));
                        thread6.Join();
                        thread6 = null;
                        thread7.Join();
                        thread7 = null;
                        thread4.Join();
                        thread4 = null;

                        Thread thread8 = CommonTool.StartBlinkThread(new Blink(
                            str1, IDStage.position[38], DrawTool.blinkTimes, DrawTool.blinkSpanTime, DrawTool.blinkHideTime, IDStage.mainForm));

                        Thread thread9 = CommonTool.StartBlinkThread(new Blink(
                            str2, IDStage.position[40], DrawTool.blinkTimes, DrawTool.blinkSpanTime, DrawTool.blinkHideTime, IDStage.mainForm));

                        Thread thread10 = CommonTool.StartBlinkThread(new Blink(
                           code.codeType.ToString(), IDStage.position[41], DrawTool.blinkTimes, DrawTool.blinkSpanTime, DrawTool.blinkHideTime, IDStage.mainForm));

                        thread8.Join();
                        thread8 = null;
                        thread9.Join();
                        thread9 = null;
                        thread10.Join();
                        thread10 = null;

                        CommonTool.DrawPath(IDStage.mainForm,
                            new DrawPath[5]{
                                 new DrawPath(mainForm.GetGraphics(),new Point[4]{IDStage.position[18],IDStage.position[19],IDStage.position[34],IDStage.position[37]},DrawTool.orignalPen),
                                 new DrawPath(mainForm.GetGraphics(),new Point[3]{IDStage.position[19],IDStage.position[14],IDStage.position[15]},DrawTool.orignalPen),
                                 new DrawPath(mainForm.GetGraphics(),new Point[2]{IDStage.position[29],IDStage.position[30]},DrawTool.orignalPen),
                                 new DrawPath(mainForm.GetGraphics(),new Point[2]{IDStage.position[20],IDStage.position[22]},DrawTool.orignalPen),
                                 new DrawPath(mainForm.GetGraphics(),new Point[2]{IDStage.position[31],IDStage.position[33]},DrawTool.orignalPen)
                            });

                        EXStage.Wait();
                        EXStage.code = IDStage.code;
                        EXStage.args = new object[2] { obj1, obj2 };
                        EXStage.enableRun++;
                        break;
                    }
                case CodeType.BEQZ:
                case CodeType.BNEZ:
                    {
                        Thread thread1 = CommonTool.StartDrawPathsThread(new DrawPaths(
                                                   new Animate[1] { 
                                CommonTool.SetAnimate(IDStage.position[18],IDStage.position[19],Direction.LeftToRight,(string)code.args[0]+"/"+str3,true,mainForm.GetGraphics())
                            }, IDStage.mainForm));
                        thread1.Join();
                        thread1 = null;

                        Thread thread2 = CommonTool.StartDrawPathsThread(new DrawPaths(
                           new Animate[2] { 
                                CommonTool.SetAnimate(IDStage.position[19],IDStage.position[14],Direction.BottomToTop,(string)code.args[0],true,mainForm.GetGraphics()),
                                CommonTool.SetAnimate(IDStage.position[14],IDStage.position[15],Direction.LeftToRight,(string)code.args[0],true,mainForm.GetGraphics())
                            }, IDStage.mainForm));

                        Thread thread3 = CommonTool.StartDrawPathsThread(new DrawPaths(
                           new Animate[1] { 
                                CommonTool.SetAnimate(IDStage.position[19],IDStage.position[29],Direction.TopToBottom,code.codeType.ToString() +"/"+ str3,true,mainForm.GetGraphics())
                            }, IDStage.mainForm));
                        thread3.Join();
                        thread3 = null;

                        Thread thread4 = CommonTool.StartDrawPathsThread(new DrawPaths(
                           new Animate[2] { 
                                CommonTool.SetAnimate(IDStage.position[29],IDStage.position[34],Direction.TopToBottom,code.codeType.ToString(),true,mainForm.GetGraphics()),
                                CommonTool.SetAnimate(IDStage.position[34],IDStage.position[37],Direction.LeftToRight,code.codeType.ToString(),true,mainForm.GetGraphics())
                            }, IDStage.mainForm));

                        Thread thread5 = CommonTool.StartDrawPathsThread(new DrawPaths(
                                                   new Animate[1] { 
                                CommonTool.SetAnimate(IDStage.position[29],IDStage.position[30],Direction.LeftToRight,str3,true,mainForm.GetGraphics())
                            }, IDStage.mainForm));
                        thread5.Join();
                        thread5 = null;
                        thread2.Join();
                        thread2 = null;

                        Thread thread6 = CommonTool.StartDrawPathsThread(new DrawPaths(
                                                   new Animate[5] { 
                                CommonTool.SetAnimate(IDStage.position[31],IDStage.position[32],Direction.LeftToRight,str2,true,mainForm.GetGraphics()),
                                CommonTool.SetAnimate(IDStage.position[32],IDStage.position[13],Direction.BottomToTop,str2,true,mainForm.GetGraphics()),
                                CommonTool.SetAnimate(IDStage.position[13],IDStage.position[10],Direction.RightToLeft,str2,true,mainForm.GetGraphics()),
                                CommonTool.SetAnimate(IDStage.position[10],IDStage.position[6],Direction.BottomToTop,str2,true,mainForm.GetGraphics()),
                                CommonTool.SetAnimate(IDStage.position[6],IDStage.position[7],Direction.LeftToRight,str2,true,mainForm.GetGraphics())
                            }, IDStage.mainForm));

                        
                        Thread thread7 = CommonTool.StartDrawPathsThread(new DrawPaths(
                                                   new Animate[3] { 
                                CommonTool.SetAnimate(IDStage.position[8],IDStage.position[9],Direction.LeftToRight,str4,true,mainForm.GetGraphics()),
                                CommonTool.SetAnimate(IDStage.position[9],IDStage.position[2],Direction.BottomToTop,str4,true,mainForm.GetGraphics()),
                                CommonTool.SetAnimate(IDStage.position[2],IDStage.position[3],Direction.LeftToRight,str4,true,mainForm.GetGraphics())
                            }, IDStage.mainForm));

                        Thread thread8 = CommonTool.StartDrawPathsThread(new DrawPaths(
                                                   new Animate[3] { 
                                CommonTool.SetAnimate(IDStage.position[20],IDStage.position[21],Direction.LeftToRight,str1,true,mainForm.GetGraphics()),
                                CommonTool.SetAnimate(IDStage.position[21],IDStage.position[11],Direction.BottomToTop,str1,true,mainForm.GetGraphics()),
                                CommonTool.SetAnimate(IDStage.position[11],IDStage.position[42],Direction.LeftToRight,str1,true,mainForm.GetGraphics())
                            }, IDStage.mainForm));
                        thread6.Join();
                        thread6 = null;
                        thread7.Join();
                        thread7 = null;
                        thread8.Join();
                        thread8 = null;

                        Thread thread9 = CommonTool.StartDrawPathsThread(new DrawPaths(
                                                   new Animate[4] { 
                                CommonTool.SetAnimate(IDStage.position[43],IDStage.position[12],Direction.LeftToRight,str6,true,mainForm.GetGraphics()),
                                CommonTool.SetAnimate(IDStage.position[12],IDStage.position[1],Direction.BottomToTop,str6,true,mainForm.GetGraphics()),
                                CommonTool.SetAnimate(IDStage.position[1],IFStage.position[1],Direction.RightToLeft,str6,true,mainForm.GetGraphics()),
                                CommonTool.SetAnimate(IFStage.position[1],IFStage.position[8],Direction.TopToBottom,str6,true,mainForm.GetGraphics())
                            }, IDStage.mainForm));

                        Thread thread10 = CommonTool.StartDrawPathsThread(new DrawPaths(
                                                   new Animate[5] { 
                                CommonTool.SetAnimate(IDStage.position[4],IDStage.position[5],Direction.LeftToRight,str5,true,mainForm.GetGraphics()),
                                CommonTool.SetAnimate(IDStage.position[5],IDStage.position[0],Direction.BottomToTop,str5,true,mainForm.GetGraphics()),
                                CommonTool.SetAnimate(IDStage.position[0],IFStage.position[0],Direction.RightToLeft,str5,true,mainForm.GetGraphics()),
                                CommonTool.SetAnimate(IFStage.position[0],IFStage.position[6],Direction.TopToBottom,str5,true,mainForm.GetGraphics()),
                                CommonTool.SetAnimate(IFStage.position[6],IFStage.position[7],Direction.LeftToRight,str5,true,mainForm.GetGraphics())
                            }, IDStage.mainForm));
                        thread9.Join();
                        thread9 = null;
                        thread10.Join();
                        thread10 = null;

                        IDStage.bzManualEvent.Set();

                        if(IFStage.ifOverOrNop && bzSuccess)
                        {
                            Thread thread16 = CommonTool.StartDrawPathsThread(new DrawPaths(
                          new Animate[1] { 
                                CommonTool.SetAnimate(IFStage.position[11],IFStage.position[12],Direction.LeftToRight,str5,true,mainForm.GetGraphics())
                            }, IDStage.mainForm));
                            thread16.Join();
                            thread16 = null;

                            Thread thread17 = CommonTool.StartDrawPathsThread(new DrawPaths(
                              new Animate[1] { 
                                CommonTool.SetAnimate(IFStage.position[12],IFStage.position[13],Direction.LeftToRight,str5,true,mainForm.GetGraphics())
                            }, IDStage.mainForm));

                            Thread thread18 = CommonTool.StartDrawPathsThread(new DrawPaths(
                              new Animate[4] { 
                                CommonTool.SetAnimate(IFStage.position[12],IFStage.position[3],Direction.BottomToTop,str5,true,mainForm.GetGraphics()),
                                CommonTool.SetAnimate(IFStage.position[3],IFStage.position[2],Direction.RightToLeft,str5,true,mainForm.GetGraphics()),
                                CommonTool.SetAnimate(IFStage.position[2],IFStage.position[16],Direction.TopToBottom,str5,true,mainForm.GetGraphics()),
                                CommonTool.SetAnimate(IFStage.position[16],IFStage.position[17],Direction.LeftToRight,str5,true,mainForm.GetGraphics())
                            }, IDStage.mainForm));

                            thread17.Join();
                            thread17 = null;
                            thread18.Join();
                            thread18 = null;
                        }

                        thread4.Join();
                        thread4 = null;

                        Thread thread11 = CommonTool.StartBlinkThread(new Blink(
                           code.codeType.ToString(), IDStage.position[41], DrawTool.blinkTimes, DrawTool.blinkSpanTime, DrawTool.blinkHideTime, IDStage.mainForm));

                        if (IFStage.ifOverOrNop && bzSuccess)
                        {
                            Thread thread19 = CommonTool.StartBlinkThread(new Blink(
                            str5, IFStage.position[24], DrawTool.blinkTimes, DrawTool.blinkSpanTime, DrawTool.blinkHideTime, IDStage.mainForm));
                            thread19.Join();
                            thread19 = null;
                        }
                        
                        thread11.Join();
                        thread11 = null;

                        CommonTool.DrawPath(IDStage.mainForm,
                            new DrawPath[8]{
                                 new DrawPath(mainForm.GetGraphics(),new Point[4]{IDStage.position[18],IDStage.position[19],IDStage.position[34],IDStage.position[37]},DrawTool.orignalPen),
                                 new DrawPath(mainForm.GetGraphics(),new Point[3]{IDStage.position[19],IDStage.position[14],IDStage.position[15]},DrawTool.orignalPen),
                                 new DrawPath(mainForm.GetGraphics(),new Point[2]{IDStage.position[29],IDStage.position[30]},DrawTool.orignalPen),
                                 new DrawPath(mainForm.GetGraphics(),new Point[4]{IDStage.position[20],IDStage.position[21],IDStage.position[11],IDStage.position[42]},DrawTool.orignalPen),
                                 new DrawPath(mainForm.GetGraphics(),new Point[6]{IDStage.position[31],IDStage.position[32],IDStage.position[13],IDStage.position[10],IDStage.position[6],IDStage.position[7]},DrawTool.orignalPen),
                                 new DrawPath(mainForm.GetGraphics(),new Point[4]{IDStage.position[8],IDStage.position[9],IDStage.position[2],IDStage.position[3]},DrawTool.orignalPen),
                                 new DrawPath(mainForm.GetGraphics(),new Point[5]{IDStage.position[43],IDStage.position[12],IDStage.position[1],IFStage.position[1],IFStage.position[8]},DrawTool.orignalPen),
                                 new DrawPath(mainForm.GetGraphics(),new Point[6]{IDStage.position[4],IDStage.position[5],IDStage.position[0],IFStage.position[0],IFStage.position[6],IFStage.position[7]},DrawTool.orignalPen)
                            });

                        if (IFStage.ifOverOrNop && bzSuccess)
                        {
                            CommonTool.DrawPath(IDStage.mainForm,
                            new DrawPath[2]{
                                 new DrawPath(mainForm.GetGraphics(),new Point[2]{IFStage.position[11],IFStage.position[13]},DrawTool.orignalPen),
                                 new DrawPath(mainForm.GetGraphics(),new Point[5]{IFStage.position[12],IFStage.position[3],IFStage.position[2],IFStage.position[16],IFStage.position[17]},DrawTool.orignalPen),
                            });
                        }

                        EXStage.Wait();
                        EXStage.code = IDStage.code;
                        EXStage.args = null;
                        EXStage.enableRun++;
                        if (IFStage.ifOverOrNop && bzSuccess)
                        {
                            IFStage.codeCurrentAddress = IDStage.bzAddress;
                            IDStage.code = new Code(CodeType.NOP, null);
                        }
                        break;
                    }
                case CodeType.SD:
                case CodeType.SB:
                case CodeType.SH:
                case CodeType.SW:
                case CodeType.SDC1:
                case CodeType.SWC1:
                    {
                        Thread thread1 = CommonTool.StartDrawPathsThread(new DrawPaths(
                                                   new Animate[1] { 
                                CommonTool.SetAnimate(IDStage.position[18],IDStage.position[19],Direction.LeftToRight,(string)code.args[0]+"/"+str3 + "/" + (string)code.args[2],true,mainForm.GetGraphics())
                            }, IDStage.mainForm));
                        thread1.Join();
                        thread1 = null;

                        Thread thread11 = CommonTool.StartDrawPathsThread(new DrawPaths(
                           new Animate[1] { 
                                CommonTool.SetAnimate(IDStage.position[19],IDStage.position[16],Direction.BottomToTop,(string)code.args[0] + "/" + (string)code.args[2],true,mainForm.GetGraphics())
                            }, IDStage.mainForm));

                        Thread thread3 = CommonTool.StartDrawPathsThread(new DrawPaths(
                           new Animate[1] { 
                                CommonTool.SetAnimate(IDStage.position[19],IDStage.position[29],Direction.TopToBottom,code.codeType.ToString() + "/" + str3,true,mainForm.GetGraphics())
                            }, IDStage.mainForm));

                        thread11.Join();
                        thread11 = null;

                        Thread thread2 = CommonTool.StartDrawPathsThread(new DrawPaths(
                           new Animate[2] { 
                                CommonTool.SetAnimate(IDStage.position[16],IDStage.position[14],Direction.BottomToTop,(string)code.args[0],true,mainForm.GetGraphics()),
                                CommonTool.SetAnimate(IDStage.position[14],IDStage.position[15],Direction.LeftToRight,(string)code.args[0],true,mainForm.GetGraphics())
                            }, IDStage.mainForm));

                        Thread thread12 = CommonTool.StartDrawPathsThread(new DrawPaths(
                           new Animate[1] { 
                                CommonTool.SetAnimate(IDStage.position[16],IDStage.position[17],Direction.LeftToRight,(string)code.args[2],true,mainForm.GetGraphics())
                            }, IDStage.mainForm));

                        thread3.Join();
                        thread3 = null;

                        Thread thread4 = CommonTool.StartDrawPathsThread(new DrawPaths(
                           new Animate[2] { 
                                CommonTool.SetAnimate(IDStage.position[29],IDStage.position[34],Direction.TopToBottom,code.codeType.ToString(),true,mainForm.GetGraphics()),
                                CommonTool.SetAnimate(IDStage.position[34],IDStage.position[37],Direction.LeftToRight,code.codeType.ToString(),true,mainForm.GetGraphics())
                            }, IDStage.mainForm));

                        Thread thread5 = CommonTool.StartDrawPathsThread(new DrawPaths(
                                                   new Animate[1] { 
                                CommonTool.SetAnimate(IDStage.position[29],IDStage.position[30],Direction.LeftToRight,str3,true,mainForm.GetGraphics())
                            }, IDStage.mainForm));
                        thread5.Join();
                        thread5 = null;
                        thread2.Join();
                        thread2 = null;
                        thread12.Join();
                        thread12 = null;

                        Thread thread6 = CommonTool.StartDrawPathsThread(new DrawPaths(
                                                   new Animate[1] { 
                                CommonTool.SetAnimate(IDStage.position[20],IDStage.position[22],Direction.LeftToRight,str1,true,mainForm.GetGraphics())
                            }, IDStage.mainForm));
                        Thread thread13 = CommonTool.StartDrawPathsThread(new DrawPaths(
                                                   new Animate[1] { 
                                CommonTool.SetAnimate(IDStage.position[27],IDStage.position[28],Direction.LeftToRight,str4,true,mainForm.GetGraphics())
                            }, IDStage.mainForm));
                        Thread thread7 = CommonTool.StartDrawPathsThread(new DrawPaths(
                                                   new Animate[1] { 
                                CommonTool.SetAnimate(IDStage.position[31],IDStage.position[33],Direction.LeftToRight,str2,true,mainForm.GetGraphics())
                            }, IDStage.mainForm));
                        thread6.Join();
                        thread6 = null;
                        thread13.Join();
                        thread13 = null;
                        thread7.Join();
                        thread7 = null;
                        thread4.Join();
                        thread4 = null;

                        Thread thread8 = CommonTool.StartBlinkThread(new Blink(
                            str1, IDStage.position[38], DrawTool.blinkTimes, DrawTool.blinkSpanTime, DrawTool.blinkHideTime, IDStage.mainForm));

                        Thread thread14 = CommonTool.StartBlinkThread(new Blink(
                            str4, IDStage.position[39], DrawTool.blinkTimes, DrawTool.blinkSpanTime, DrawTool.blinkHideTime, IDStage.mainForm));

                        Thread thread9 = CommonTool.StartBlinkThread(new Blink(
                            str2, IDStage.position[40], DrawTool.blinkTimes, DrawTool.blinkSpanTime, DrawTool.blinkHideTime, IDStage.mainForm));

                        Thread thread10 = CommonTool.StartBlinkThread(new Blink(
                           code.codeType.ToString(), IDStage.position[41], DrawTool.blinkTimes, DrawTool.blinkSpanTime, DrawTool.blinkHideTime, IDStage.mainForm));

                        thread8.Join();
                        thread8 = null;
                        thread14.Join();
                        thread14 = null;
                        thread9.Join();
                        thread9 = null;
                        thread10.Join();
                        thread10 = null;

                        CommonTool.DrawPath(IDStage.mainForm,
                            new DrawPath[7]{
                                 new DrawPath(mainForm.GetGraphics(),new Point[4]{IDStage.position[18],IDStage.position[19],IDStage.position[34],IDStage.position[37]},DrawTool.orignalPen),
                                 new DrawPath(mainForm.GetGraphics(),new Point[3]{IDStage.position[19],IDStage.position[14],IDStage.position[15]},DrawTool.orignalPen),
                                 new DrawPath(mainForm.GetGraphics(),new Point[2]{IDStage.position[29],IDStage.position[30]},DrawTool.orignalPen),
                                 new DrawPath(mainForm.GetGraphics(),new Point[2]{IDStage.position[20],IDStage.position[22]},DrawTool.orignalPen),
                                 new DrawPath(mainForm.GetGraphics(),new Point[2]{IDStage.position[31],IDStage.position[33]},DrawTool.orignalPen),
                                 new DrawPath(mainForm.GetGraphics(),new Point[2]{IDStage.position[16],IDStage.position[17]},DrawTool.orignalPen),
                                 new DrawPath(mainForm.GetGraphics(),new Point[2]{IDStage.position[27],IDStage.position[28]},DrawTool.orignalPen)
                            });

                        EXStage.Wait();
                        EXStage.code = IDStage.code;
                        EXStage.args = new object[3] { obj1, obj2, obj3 };
                        EXStage.enableRun++;
                        break;
                    }
            }
            manuaEevent.Set();

        }

        static public void Start(MainForm param)
        {
            mainForm = param;
            //开始标志
            manuaEevent.Reset();
            registerReadManualEvent.Reset();
            idDrawManualEvent.Reset();
            idThread = new Thread(new ThreadStart(ThreadFun));
            idThread.Start();
        }

        static public void Initialize()
        {
            IDStage.enableRun = -1;
            nopNum = 0;
            runTimes = 0;
            dicTmp = new Dictionary<string, int>();
            manuaEevent = new ManualResetEvent(false);
            bzManualEvent = new ManualResetEvent(false);
            nopNumManualEvent = new ManualResetEvent(false);
            nopDrawManualEvent = new ManualResetEvent(false);
            registerReadManualEvent = new ManualResetEvent(false);
            idDrawManualEvent = new ManualResetEvent(false);
            bzAddress = 0;
            bzSuccess = false;
            code = new Code(CodeType.NOP, null);
        }

        static public void Wait()
        {
            manuaEevent.WaitOne();
        }

        static public void Close()
        {
            if (idThread != null && idThread.IsAlive)
            {
                idThread.Join();
            }
        }

        static public int VlidateDataConflict( Code code)
        {
               int i = IDStage.runTimes;
                switch (code.codeType)
                {
                    case CodeType.ADD:
                    case CodeType.ADDU:
                    case CodeType.SUB:
                    case CodeType.SUBU:
                    case CodeType.MUL:
                    case CodeType.SLT:
                    case CodeType.SLTU:
                    case CodeType.DADD:
                    case CodeType.DADDU:
                    case CodeType.DSUB:
                    case CodeType.DSUBU:
                    case CodeType.AND:
                    case CodeType.NOR:
                    case CodeType.OR:
                    case CodeType.XOR:
                    case CodeType.ADD_S:
                    case CodeType.ADD_D:
                    case CodeType.SUB_S:
                    case CodeType.SUB_D:
                    case CodeType.MUL_S:
                    case CodeType.MUL_D:
                    case CodeType.DIV_S:
                    case CodeType.DIV_D:
                    case CodeType.SLLV:
                    case CodeType.SRAV:
                    case CodeType.SRLV:
                    case CodeType.DSLLV:
                    case CodeType.DSRLV:
                    case CodeType.DSRAV:
                        {
                            int t1 = (dicTmp.ContainsKey((string)code.args[0]) && dicTmp[(string)code.args[0]] + 3 > i) ? dicTmp[(string)code.args[0]] + 3 - i : 0;
                            int t2 = (dicTmp.ContainsKey((string)code.args[1])) && (dicTmp[(string)code.args[1]] + 3 > i) ? dicTmp[(string)code.args[1]] + 3 - i : 0;
                            int num = Math.Max(t1, t2);
                            if (num <= 0)
                            {
                                if (dicTmp.ContainsKey((string)code.args[2])) dicTmp[(string)code.args[2]] = i;
                                else dicTmp.Add((string)code.args[2], i);
                            }
                            return num;
                        }
                    case CodeType.ADDI:
                    case CodeType.ADDIU:
                    case CodeType.SLTI:
                    case CodeType.SLTIU:
                    case CodeType.DADDI:
                    case CodeType.DADDIU:
                    case CodeType.ANDI:
                    case CodeType.ORI:
                    case CodeType.XORI:
                    case CodeType.SLL:
                    case CodeType.SRA:
                    case CodeType.SRL:
                    case CodeType.DSLL:
                    case CodeType.DSRL:
                    case CodeType.DSRA:
                        {
                            int t1 = (dicTmp.ContainsKey((string)code.args[0]) && dicTmp[(string)code.args[0]] + 3 > i) ? dicTmp[(string)code.args[0]] + 3 - i : 0;
                            if (t1 <= 0)
                            {
                                if (dicTmp.ContainsKey((string)code.args[2])) dicTmp[(string)code.args[2]] = i;
                                else dicTmp.Add((string)code.args[2], i);
                            }
                            return t1;
                        }
                    case CodeType.BEQZ:
                    case CodeType.BNEZ:
                        {
                            int t1 = (dicTmp.ContainsKey((string)code.args[0]) && dicTmp[(string)code.args[0]] + 3 > i) ? dicTmp[(string)code.args[0]] + 3 - i : 0;
                            return t1;
                        }
                    case CodeType.LB:
                    case CodeType.LH:
                    case CodeType.LW:
                    case CodeType.LBU:
                    case CodeType.LHU:
                    case CodeType.LWU:
                    case CodeType.LD:
                    case CodeType.SD:
                    case CodeType.SB:
                    case CodeType.SH:
                    case CodeType.SW:
                    case CodeType.LDC1:
                    case CodeType.LWC1:
                    case CodeType.SDC1:
                    case CodeType.SWC1:
                        {
                            int t1 = (dicTmp.ContainsKey((string)code.args[0]) && dicTmp[(string)code.args[0]] + 3 > i) ? dicTmp[(string)code.args[0]] + 3 - i : 0;
                            if (t1 <= 0)
                            {
                                if (dicTmp.ContainsKey((string)code.args[2])) dicTmp[(string)code.args[2]] = i;
                                else dicTmp.Add((string)code.args[2], i);
                            }
                            return t1;
                        }
                }
                return 0;
            }
    }
}
